package com.scwang.smart.drawer.exercise.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.google.gson.GsonBuilder
import com.scwang.smart.drawer.app.R
import com.scwang.smart.drawer.exercise.adapter.ExerciseAdapter
import com.scwang.smart.drawer.exercise.model.Example
import org.json.JSONObject
import java.io.BufferedReader
import java.io.InputStreamReader
import java.nio.charset.StandardCharsets


/**
 * A simple [Fragment] subclass.
 * create an instance of this fragment.
 */
class ExerciseSkeletonFragment : Fragment() {

    private lateinit var mExercise: Example
    private lateinit var mViewPager: ViewPager2
    private lateinit var mAdapter: ExerciseAdapter

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_exercise_skeleton, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mViewPager = view.findViewById(R.id.skeleton_pager)
        resources.openRawResource(R.raw.example).use {
            val gson = GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create()
            mExercise = gson.fromJson(InputStreamReader(it, StandardCharsets.UTF_8), Example::class.java)
            mAdapter = ExerciseAdapter(mExercise)
            mViewPager.adapter = mAdapter
        }
    }
}